/**
 * Copyright (C), 2020, 安徽贝慕信息科技有限公司
 * FileName: RedisTest
 * Author:   Allen
 * Date:     2020/8/25 14:28
 * Description: 测试Redis存储数据转换
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.bim.marvel.openapi;

import com.bim.marvel.OpenApiApplication;
import com.bim.marvel.openapi.client.dto.AppDetailDTO;
import com.bim.marvel.openapi.client.dto.BackendApiDTO;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.Date;

/**
 * 〈测试Redis存储数据转换〉
 *
 * @author Allen
 * @date 2020/8/25
 * @since 1.0.0
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = OpenApiApplication.class)
@TestPropertySource(properties = "spring.profiles.active: dev")
@EnableAutoConfiguration
@Slf4j
public class RedisTest {

    /**
     * 获得开放平台api地址常量
     */
    public static final String GET_OPEN_API_URI = "getOpenApiUri::openApiUrl:";

    /**
     * 连接符
     */
    public static final String GET_OPEN_API_CONNECTOR = "_";

    /**
     * 注入RedisTemplate
     */
    @Autowired
    private RedisTemplate<Object, Object> template;

    /**
     * 测试生成密钥信息
     */
    @Test
    public void savereids() {
        AppDetailDTO appDetailDTO = new AppDetailDTO();
        appDetailDTO.setAppId("feiyun");
        appDetailDTO.setAppSecret("1264176236075573249");
        appDetailDTO.setSessionKey("9aeaa3c9af2e73d410472824f9df2a4d");
        appDetailDTO.setExpiredDate(new Date());
        template.opsForValue().set("getAppDetailInfo:" + appDetailDTO.getAppId(), appDetailDTO);
        AppDetailDTO result = (AppDetailDTO) template.opsForValue().get("getAppDetailInfo" + appDetailDTO.getAppId());
        log.info(result.getAppId());
    }

    /**
     * 测试boundValueOps方式的存储
     */
    @Test
    public void saveHashReids() {
        AppDetailDTO appDetailDTO = new AppDetailDTO();
        appDetailDTO.setAppId("feiyun");
        appDetailDTO.setAppSecret("1264176236075573249");
        appDetailDTO.setSessionKey("9aeaa3c9af2e73d410472824f9df2a4d");
        appDetailDTO.setExpiredDate(new Date());
        template.boundValueOps("getAppDetailInfo:" + appDetailDTO.getAppId()).set(appDetailDTO);
        AppDetailDTO result = (AppDetailDTO) template.boundValueOps("getAppDetailInfo"
                + appDetailDTO.getAppId()).get();
        log.info(result.getAppId());
    }

    /**
     * 测试获得后端开放平台API接口地址缓存信息
     */
    @Test
    public void getBackendApiExist() {
        BackendApiDTO backendApiDTO = new BackendApiDTO();
        backendApiDTO.setName("日志信息接口");
        backendApiDTO.setServiceId("xysy-marvel");
        backendApiDTO.setPath("/gateway/marvel/api/log/list");
        Boolean backendApiExist = template.hasKey(GET_OPEN_API_URI + backendApiDTO.getServiceId()
               + GET_OPEN_API_CONNECTOR + backendApiDTO.getPath());
        log.info("backendApiExist--> {}", backendApiExist);
    }
}
